package org.fig.dependency.injection;

import org.fig.ioc.overview.domain.User;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @Description: 基于注解配置的 Constructor注入
 * @Author: Flynn Vanderboom
 * @Data: 2021/6/20 22:18
 */

public class AnnotationDependencyConstructorInjectionDemo {
    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //注册  配置类
        context.register(AnnotationDependencyConstructorInjectionDemo.class);



        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);

        String xmlResourcePath = "classpath:META-INF/dependency-lookup-context.xml";

        //加载XML资源，解析并且生成BeanDefinition
        reader.loadBeanDefinitions(xmlResourcePath);



        //启动上下文
        context.refresh();

        
/*        Map<String,UserHolder> userHolderBeans = context.getBeansOfType(UserHolder.class);

        System.out.println(userHolderBeans);*/
        
        UserHolder userHolder = context.getBean(UserHolder.class);

        System.out.println(userHolder);

        //关闭上下文
        context.close();
    }



    @Bean
    public UserHolder userHolder(User user) {
        return new UserHolder(user);
    }



}
